package com.ktgj.pay.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.ktgj.common.core.annotation.Excel;
import com.ktgj.common.core.domain.R;
import com.ktgj.customer.api.RemoteCustomerService;
import com.ktgj.customer.api.domain.Customer;
import com.ktgj.customer.api.domain.Employee;
import com.ktgj.pay.domain.*;
import com.ktgj.pay.domain.vo.InvoiceInvoiceOrderVO;
import com.ktgj.pay.domain.vo.OperationRecordVo;
import com.ktgj.pay.mapper.InvoiceCheckingMapper;
import com.ktgj.pay.mapper.InvoiceDeliveryMapper;
import com.ktgj.pay.mapper.InvoiceOperatelogMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.pay.mapper.InvoiceInvoiceOrderMapper;
import com.ktgj.pay.service.IInvoiceInvoiceOrderService;

/**
 * 发票订单Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-01-24
 */
@Service
public class InvoiceInvoiceOrderServiceImpl implements IInvoiceInvoiceOrderService 
{
    @Autowired
    private InvoiceInvoiceOrderMapper invoiceInvoiceOrderMapper;

    @Autowired
    private InvoiceOperatelogMapper invoiceOperatelogMapper;

    @Autowired
    private InvoiceDeliveryMapper invoiceDeliveryMapper;

    @Autowired
    private InvoiceCheckingMapper invoiceCheckingMapper;

    @Autowired
    private RemoteCustomerService remoteCustomerService;

    /**
     * 查询发票订单
     * 
     * @param invoiceid 发票订单ID
     * @return 发票订单
     */
    @Override
    public InvoiceInvoiceOrder selectInvoiceInvoiceOrderById(Long invoiceid)
    {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        InvoiceInvoiceOrder invoiceInvoiceOrder = invoiceInvoiceOrderMapper.selectInvoiceInvoiceOrderById(invoiceid);
        if (invoiceInvoiceOrder != null){
            List<InvoiceOperatelog> invoiceOperatelogs = invoiceOperatelogMapper.invoiceOperatelogList(Long.valueOf(invoiceInvoiceOrder.getInvoiceid()));
            if (invoiceOperatelogs.size() > 0){
                List<OperationRecordVo> operationRecordVoList = new ArrayList<>();
                for (InvoiceOperatelog invoiceOperatelog : invoiceOperatelogs) {
                    OperationRecordVo operationRecordVo = new OperationRecordVo();
                    operationRecordVo.setId(invoiceOperatelog.getId());
                    if (invoiceOperatelog.getCreateTime() != null){
                        operationRecordVo.setTime(format.format(invoiceOperatelog.getCreateTime()));
                    }

                    operationRecordVo.setName(invoiceOperatelog.getOperater());
                    operationRecordVo.setIllustrate(invoiceOperatelog.getOperaterdesc());
                    operationRecordVoList.add(operationRecordVo);
                }
                invoiceInvoiceOrder.setOperationRecordVoList(operationRecordVoList);
            }

            InvoiceDelivery invoiceDeliverie = invoiceDeliveryMapper.invoiceDeliveryList(invoiceInvoiceOrder.getInvoiceid());
            if (invoiceDeliverie != null){
                invoiceInvoiceOrder.setInvoiceDeliverie(invoiceDeliverie);
            }
        }
        return invoiceInvoiceOrder;
    }

    @Override
    public InvoiceInvoiceOrder selectInvoiceInvoiceOrderByNum(Long orderNum) {
        return invoiceInvoiceOrderMapper.selectInvoiceInvoiceOrderByNum(orderNum);
    }

    /**
     * 查询发票订单列表
     *
     * @param jsjid 发票订单
     * @return 发票订单集合
     */   public List<InvoiceInvoiceOrder> selectInvoiceInvoiceOrderListByCustomerId(String jsjid )
    {
        return  invoiceInvoiceOrderMapper.selectInvoiceInvoiceOrderListByCustomerId(jsjid);
    }
    /**
     * 查询发票订单列表
     * 
     * @param invoiceInvoiceOrder 发票订单
     * @return 发票订单
     */
    @Override
    public List<InvoiceInvoiceOrder> selectInvoiceInvoiceOrderList(InvoiceInvoiceOrder invoiceInvoiceOrder)
    {
        return invoiceInvoiceOrderMapper.selectInvoiceInvoiceOrderList(invoiceInvoiceOrder);
    }

    /**
     * 新增发票订单
     * 
     * @param invoiceInvoiceOrder 发票订单
     * @return 结果
     */
    @Override
    public int insertInvoiceInvoiceOrder(InvoiceInvoiceOrder invoiceInvoiceOrder)
    {
        InvoiceOperatelog operatelog = new InvoiceOperatelog();
        InvoiceChecking checking = new InvoiceChecking();
        int order = invoiceInvoiceOrderMapper.insertInvoiceInvoiceOrder(invoiceInvoiceOrder);
        if (order > 0) {
            operatelog.setInvoiceid(Long.valueOf(invoiceInvoiceOrder.getInvoiceid()));
            operatelog.setOperater("系统管理员");
            operatelog.setOperatertime(new Date());
            operatelog.setOperaterdesc("开票信息-保存成功");
            invoiceOperatelogMapper.insertInvoiceOperatelog(operatelog);
            if (invoiceInvoiceOrder.getInvoiceDeliverie() != null) {
                InvoiceDelivery deliverie = invoiceInvoiceOrder.getInvoiceDeliverie();
                deliverie.setInvoiceid(invoiceInvoiceOrder.getInvoiceid());
                int invoiceDelivery = invoiceDeliveryMapper.insertInvoiceDelivery(deliverie);
                if (invoiceDelivery > 0) {
                    operatelog.setInvoiceid(Long.valueOf(invoiceInvoiceOrder.getInvoiceid()));
                    operatelog.setOperater("系统管理员");
                    operatelog.setOperatertime(new Date());
                    operatelog.setOperaterdesc("开票信息-子订单状态-回填成功");
                    invoiceOperatelogMapper.insertInvoiceOperatelog(operatelog);
                    invoiceInvoiceOrder.setSuborders(String.valueOf(deliverie.getDeliveryid()));
                    invoiceInvoiceOrderMapper.updateInvoiceInvoiceOrder(invoiceInvoiceOrder);
                }
            }
            checking.setNewinvoiceid(Long.valueOf(invoiceInvoiceOrder.getInvoiceid()));
            checking.setChecktype(1L);
            checking.setCheckstate(1L);
            checking.setCreateTime(new Date());
            checking.setApplicantid(invoiceInvoiceOrder.getJsjid());
            checking.setOperator("0");
            int invoiceChecking = invoiceCheckingMapper.insertInvoiceChecking(checking);
            if (invoiceChecking > 0) {
                operatelog.setInvoiceid(Long.valueOf(invoiceInvoiceOrder.getInvoiceid()));
                operatelog.setOperater("系统管理员");
                operatelog.setOperatertime(new Date());
                operatelog.setOperaterdesc("审核信息-收款审核信息-新增成功");
                invoiceOperatelogMapper.insertInvoiceOperatelog(operatelog);
            }
        }
        return order;
    }

    /**
     * 修改发票订单
     * 
     * @param invoiceInvoiceOrder 发票订单
     * @return 结果
     */
    @Override
    public int updateInvoiceInvoiceOrder(InvoiceInvoiceOrder invoiceInvoiceOrder)
    {
        return invoiceInvoiceOrderMapper.updateInvoiceInvoiceOrder(invoiceInvoiceOrder);
    }

    /**
     * 批量删除发票订单
     * 
     * @param invoiceids 需要删除的发票订单ID
     * @return 结果
     */
    @Override
    public int deleteInvoiceInvoiceOrderByIds(Long[] invoiceids)
    {
        return invoiceInvoiceOrderMapper.deleteInvoiceInvoiceOrderByIds(invoiceids);
    }

    /**
     * 删除发票订单信息
     * 
     * @param invoiceid 发票订单ID
     * @return 结果
     */
    @Override
    public int deleteInvoiceInvoiceOrderById(Long invoiceid)
    {
        return invoiceInvoiceOrderMapper.deleteInvoiceInvoiceOrderById(invoiceid);
    }

    //发票查询
    @Override
    public List<InvoiceInvoiceOrderDTO> invoiceOrderList(InvoiceInvoiceOrderVO invoiceOrder) {
        Calendar calendar = new GregorianCalendar();
        if (invoiceOrder.getCreateTime() != null) {
            if (invoiceOrder.getCreateTimeEnd() != null) {
                calendar.setTime(invoiceOrder.getCreateTime());
                calendar.add(Calendar.DATE, 1);
                invoiceOrder.setCreateTime(calendar.getTime());
                calendar.setTime(invoiceOrder.getCreateTimeEnd());
                calendar.add(Calendar.DATE, 1);
                invoiceOrder.setCreateTimeEnd(calendar.getTime());
            }
        }
        if (invoiceOrder.getActualtime() != null) {
            if (invoiceOrder.getActualtimeEnd() != null) {
                calendar.setTime(invoiceOrder.getActualtime());
                calendar.add(Calendar.DATE, 1);
                invoiceOrder.setActualtime(calendar.getTime());
                calendar.setTime(invoiceOrder.getActualtimeEnd());
                calendar.add(Calendar.DATE, 1);
                invoiceOrder.setActualtimeEnd(calendar.getTime());
            }
        }
        List<InvoiceInvoiceOrderDTO> invoiceOrderList = invoiceInvoiceOrderMapper.invoiceOrderList(invoiceOrder);
        for (int i = 0; i < invoiceOrderList.size(); i++) {
            R<Customer> customer = remoteCustomerService.getCustomerId(Long.valueOf(invoiceOrderList.get(i).getJsjid()));
            if (customer.getCode() == 200 && customer.getData() != null)
                invoiceOrderList.get(i).setJsjidName(customer.getData().getCustomerName());
            else
                invoiceOrderList.get(i).setJsjidName(invoiceOrderList.get(i).getJsjid());
            List<InvoiceChecking> invoiceCheckings = invoiceCheckingMapper.queryInvoiceCheckingList(Long.valueOf(invoiceOrderList.get(i).getInvoiceid()));
            List<InvoiceDelivery> invoiceDeliveries = invoiceDeliveryMapper.queryInvoiceDeliveryList(invoiceOrderList.get(i).getInvoiceid());
            if (invoiceCheckings != null && invoiceCheckings.size() > 0) {
                invoiceOrderList.get(i).setCheckstate(invoiceCheckings.get(invoiceCheckings.size() - 1).getCheckstate());
                invoiceOrderList.get(i).setOperator(invoiceCheckings.get(invoiceCheckings.size() - 1).getOperator());
                if (!invoiceCheckings.get(invoiceCheckings.size() - 1).getOperator().equals("0")) {
                    Map<String, Long> map = new HashMap<>();
                    map.put("employeeId", Long.valueOf(invoiceCheckings.get(invoiceCheckings.size() - 1).getOperator()));
                    R<Employee> infoById = remoteCustomerService.getInfoById(map);
                    if (infoById.getCode() == 200 && infoById.getData() != null)
                        invoiceOrderList.get(i).setOperatorName(infoById.getData().getEmployeeName());
                    else
                        invoiceOrderList.get(i).setOperatorName(invoiceCheckings.get(invoiceCheckings.size() - 1).getOperator());
                } else {
                    invoiceOrderList.get(i).setOperatorName(invoiceCheckings.get(invoiceCheckings.size() - 1).getOperator());
                }
            }
            if (invoiceDeliveries != null && invoiceDeliveries.size() > 0) {
                invoiceOrderList.get(i).setEmailstate(invoiceDeliveries.get(invoiceDeliveries.size() - 1).getEmailstate());
            }
        }
        return invoiceOrderList;
    }
}
